home *** CD-ROM | disk | FTP | other *** search
/ Robotics & Artificial Int…3 (Professional Edition) / Robotics & Artificial Intelligence Tools 2003 (Professional Edition).iso / neural network tool and application / nsinstall.exe / data1.cab / DLLCust_Files / FILE / BINUSH.C < prev    next >
Encoding:
C/C++ Source or Header  |  2002-03-08  |  1.9 KB  |  65 lines

  1. // Dynamic link library implementation of binary file translator with offset and duration
  2.  
  3. #include "NSDLL.h" 
  4. #include <stdio.h> 
  5.  
  6. /**********************************/
  7. /* Read next sample from file */
  8. __declspec(dllexport) BOOL performFile(
  9.     DLLData        *instance,    // Pointer to instance data (may be NULL)
  10.     FILE        *file,         // Pointer to the opened file
  11.     NSFloat        *sample        // Location to place next sample
  12.     )
  13. {
  14.     int duration = getIntParameter(instance, 2, 1);
  15.     int *durationCount = (int*)getUserData(instance);
  16.     unsigned short typedSample;
  17.     if ((!duration || ((*durationCount)++ < duration)) && fread(&typedSample, sizeof(unsigned short), 1, (FILE*)file))    {
  18.         *sample = (NSFloat)typedSample;
  19.         return TRUE;
  20.     }
  21.     *durationCount = 0;
  22.     fclose((FILE*)file);
  23.     return FALSE;
  24. }
  25.  
  26. /********************************************/
  27. /* Open and the file and return its pointer */
  28. __declspec(dllexport) FILE *openFile(DLLData *instance, const char *filePath)
  29. {
  30.     unsigned short *buffer;
  31.     FILE *file = fopen(filePath, "rb");
  32.     int offset = getIntParameter(instance, 1, 1);
  33.  
  34.     if (offset) {
  35.         buffer = malloc(offset*sizeof(unsigned short));
  36.         fread(buffer, sizeof(unsigned short), offset, file);
  37.         free(buffer);
  38.     }
  39.     return file;
  40. }
  41.  
  42. /******************************************/
  43. /* Management of instance data (OPTIONAL) */
  44.  
  45. __declspec(dllexport) DLLData *allocFile(
  46.     DLLData    *oldInstance    // Pointer to the last instance if reallocating
  47.     )
  48. {
  49.     DLLData *instance = allocDLLInstance(oldInstance);
  50.     setParameterName(instance, 1, 1, "Offset", FALSE);
  51.     setIntParameter(instance, 1, 1, 0, FALSE);
  52.     setParameterName(instance, 2, 1, "Duration", FALSE);
  53.     setIntParameter(instance, 2, 1, 0, FALSE);
  54.     setUserData(instance, calloc(1,sizeof(int)));
  55.     return instance;
  56. }
  57.  
  58. __declspec(dllexport) void freeFile(DLLData *instance)
  59. {
  60.     if (getUserData(instance))
  61.         free((int*)getUserData(instance));
  62.     freeDLLInstance(instance);
  63. }
  64.  
  65.